<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Data_guru extends CI_Controller
{
    public function index()
    {
        $data['rslt_data_guru'] = $this->db->query('SELECT d.id, d.nama_guru, d.nomor_induk, d.gol_pangkat, d.pos_jabatan FROM data_guru d ORDER BY d.id');
        $this->template->admin('admin/v_data_guru', $data, 2, "Data Guru");
    }
	
	public function tambah()
	{
		$this->load->library('form_validation');
		$this->_rule_validation();
		if ($this->form_validation->run() === FALSE)
		{
			$data['id'] = '';
			$data['nama_lengkap'] = '';
			$data['jenis_kelamin'] = '';
			$data['nomor_induk'] = '';
			$data['jabatan'] = '';
			$data['golongan'] = '';
			$data['alamat'] = '';
			$data['nomor'] = '';
			$this->template->admin('admin/v_data_guru_ubah', $data, 2, "Form Tambah Data Guru");
		}
		else
		{
			$this->_simpan('TAMBAH');
		}	
	}
    
    public function edit($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('data_guru')->where('id', $id)->limit(1)->order_by('id', 'ASC')->get();
            if($rslt_data->num_rows() == 1)
            {
			
				$this->load->library('form_validation');
				$this->_rule_validation();
				if ($this->form_validation->run() === FALSE)
				{
					$rslt_data = $rslt_data->row();
					$data['id'] = $rslt_data->id;
					$data['nama_lengkap'] = $rslt_data->nama_guru;
					$data['nomor_induk'] = $rslt_data->nomor_induk;
					$data['golongan'] = $rslt_data->gol_pangkat;
					$data['jabatan'] = $rslt_data->pos_jabatan;
					$data['jenis_kelamin'] = $rslt_data->jenis_kelamin;
					$data['alamat'] = $rslt_data->alamat;
					$data['nomor'] = $rslt_data->telepon;
					$this->template->admin('admin/v_data_guru_ubah', $data, 2, "Form Ubah Data Guru");
				}
				else
				{
					$this->_simpan('UBAH');
				}
            }
            else
            {
                show_404();
            }
        }
    }
    
    public function hapus($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $this->db->where('id', $id)->limit(1)->delete('data_guru');
            redirect('admin/data_guru');
        }
    }
    
    
    public function _simpan($proses = null)
    {
        if($proses == null)
        {
            show_404();
        }
        else
        {
            $nama_lengkap = $this->input->post('nama_lengkap');
            $jenis_kelamin = $this->input->post('jenis_kelamin');
            $nomor_induk = $this->input->post('nomor_induk');
            $golongan = $this->input->post('golongan');
            $jabatan = $this->input->post('jabatan');
            $nomor = $this->input->post('nomor');
            $alamat = $this->input->post('alamat');
			$data_simpan = array('nama_guru' => $nama_lengkap,
                            'nomor_induk' => $nomor_induk,
                            'gol_pangkat' => $golongan,
                            'pos_jabatan' => $jabatan,
                            'jenis_kelamin' => $jenis_kelamin,
                            'alamat' => $alamat,
                            'telepon' => $nomor);
            if($proses == 'TAMBAH')
            {
                $this->db->insert('data_guru', $data_simpan);
            }
            elseif($proses == 'UBAH')
            {
				$id = $this->input->post('id');
                $this->db->set($data_simpan)
                    ->where('id', $id)
                    ->update('data_guru');
            }
            else
            {
                show_404();
            }
            
            redirect('admin/data_guru');
        }
    }
	
	private function _rule_validation()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
		$this->form_validation->set_rules('nama_lengkap', 'Nama Guru', 'required|max_length[50]|trim');
		$this->form_validation->set_rules('nomor_induk', 'Nomor Induk Guru', 'required|max_length[30]|trim');
		$this->form_validation->set_rules('golongan', 'Golongan Pangkat', 'required|max_length[10]|trim');
		$this->form_validation->set_rules('jabatan', 'Jabatan', 'required|max_length[30]|trim');
		$this->form_validation->set_rules('jenis_kelamin', 'Jenis Kelamin', 'required|max_length[20]|trim');
		$this->form_validation->set_rules('alamat', 'Alamat', 'required|max_length[50]|trim');
		$this->form_validation->set_rules('nomor', 'Telepon', 'required|max_length[20]|trim');
	}
}

/* End of file data_guru.php */
/* Location: ./application/controllers/admin/data_guru.php */